/*
 * Ajax controls plugin for jquery
 * version: 0.8 beta (29/10/2010)
 *
 * @requires jQuery v1.4 or later
 * @requires jquery.ui.dialog plugin
 * @requires jquery.lightbox plugin
 * 
 * @author nico.rehwaldt
 */
(function($) {
    $.extend($.fn, {
        ajaxControls: function(options) {
            var element = this;
            
            $.each($.fn.ajaxControls.controls, function(selector, action) {
                element.find(selector)
                       .each(action)
                       .end();
            });

            return this;
        }
    });

    $.fn.ajaxControls.controls = {

        "a.ajax": function(event) {
            var a = $(this);
            var dialog = $("<div" +
                (a.attr("rel") ? (" id='" + a.attr("rel") + "'") : "")
                + "></div>");
            
            var options = {
                modal: true,
                content: a.attr("href"),
                buttons: {
                    Cancel: function() {$(this).dialog("close");}
                }
            };
            
            if (a.hasClass("open-lazy")) {
                options.autoOpen = false;
            }

            if (a.attr("title")) {
                options.title = a.attr("title");
            }

            a.click(function(event) {
                event.preventDefault();
                dialog.lightbox(options);
            });
        }
    };
})(jQuery);